package com.jike.noobmoney.mvp.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.TaskListAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.TaskListEntity;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.mvp.view.activity.TaskDetailActivity;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.Logger;
import com.jike.noobmoney.util.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 游戏大厅列表
 */
public class TaskListFragment extends BaseNewFragment implements IView {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty)
    ImageView empty;

    public static String Channels_id = "Channels_id";

    private TaskPresenter taskPresenter;
    private TaskListAdapter adapter;
    private List<TaskListEntity.TaskBean> mData = new ArrayList<>();
    private boolean isRefresh = true;
    private int page = 1;
    private String c_id = "1";
    private String userid;


    @Override
    protected void onFragmentResume() {
        super.onFragmentResume();
        Logger.e("xuke","sssss");
    }

    @Override
    protected int layoutResID() {
        return R.layout.fragment_task_list;
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        mHasLoadedOnce = true;
        taskPresenter = new TaskPresenter(this);
        initData();
    }

    protected void initData() {
        adapter = new TaskListAdapter(context);

        Bundle bundle = getArguments();
        if (bundle != null) {
            c_id = bundle.getString(TaskListFragment.Channels_id);
        }

        //manager
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        getData(true);

        //item点击
        adapter.setOnItemClickListener((adapterView, view, position, l) -> {
            TaskListEntity.TaskBean item = (TaskListEntity.TaskBean) adapter.getItem(position);
            Intent intent = new Intent(context, TaskDetailActivity.class);
            intent.putExtra(TaskDetailActivity.t_id, "" + item.getT_id());
            startActivity(intent);
        });

        refreshLayout.setOnRefreshListener(refreshlayout -> {
            getData(true);
        });

        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            getData(false);
        });


    }

    /**
     * 获取数据
     */
    private void getData(boolean isRefresh) {
        this.isRefresh = isRefresh;
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        showProgressDialog();
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        taskPresenter.getTasklist(userid,c_id, "" + page, "10","","", ConstantValue.RequestKey.task_list);
    }


    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        disLoading();
        if (ConstantValue.RequestKey.task_list.equals(actionType)) {
            List<TaskListEntity.TaskBean> task = (List<TaskListEntity.TaskBean>) data;
            if (task != null && task.size() > 0) {
                if (isRefresh) {
                    mData.clear();
                    mData.addAll(task);
                } else {
                    mData.addAll(task);
                }
            }

            if (empty != null) {
                if (mData.size() > 0) {
                    empty.setVisibility(View.GONE);
                } else {
                    empty.setVisibility(View.VISIBLE);
                }
            }
            adapter.refresh(mData);
        }


    }

    @Override
    public void onFailed(String code, String msg) {
        disLoading();
    }

    private void disLoading() {
        dismissProgressDialog();
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }
}
